from OCC.Core.AIS import AIS_Shape
from OCC.Core.BRepGProp import brepgprop_VolumeProperties
from OCC.Core.GProp import GProp_GProps
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_stl_file
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.Core.gp import gp_Trsf, gp_OY, gp_Vec
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeSolid
from OCC.Core.TopoDS import topods, TopoDS_Shape


def calc_volume(shape):
    props = GProp_GProps()
    brepgprop_VolumeProperties(shape, props)
    return props.Mass()


shapes = read_stl_file('./box.stl')
shell = topods.Shell(shapes)

ms = BRepBuilderAPI_MakeSolid()
ms.Add(shell)
solid = ms.Solid()

print(solid)
print("原始体积: ", calc_volume(solid))

my_box = BRepPrimAPI_MakeBox(100, 100, 100).Shape()
my_box_transform = gp_Trsf()
my_box_transform.SetTranslation(gp_Vec(0, 0, -50))
my_box.Location(TopLoc_Location(my_box_transform))
my_box_cut = BRepAlgoAPI_Cut(solid, my_box)
my_box_result = my_box_cut.Shape()
print("用立方体切割: ", calc_volume(my_box_result))

my_ball = BRepPrimAPI_MakeSphere(5).Shape()
my_ball_transform = gp_Trsf()
my_ball_transform.SetTranslation(gp_Vec(0, 0, 5))
my_ball.Location(TopLoc_Location(my_ball_transform))
my_ball_cut = BRepAlgoAPI_Cut(solid, my_ball)
my_ball_result = my_ball_cut.Shape()
print("用球体切割: ", calc_volume(my_ball_result))
my_ball_ais_shp = AIS_Shape(TopoDS_Shape(my_ball_result))
my_ball_ais_shp.SetWidth(2)
my_ball_ais_shp.SetTransparency(0.1)

display, start_display, add_menu, add_function_to_menu = init_display()

# display.DisplayShape(solid)
# display.DisplayShape(my_ball)
# display.DisplayShape(my_ball_result)


# display.DisplayShape(my_box)
# display.DisplayShape(result)


# ais_context = display.GetContext()
# ais_context.Display(my_ball_ais_shp, True)
# display.View_Iso()

start_display()
